package com.trs.util;

import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpResponse;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;

public class HttpKeepAliveStrategy implements ConnectionKeepAliveStrategy {

	@Override
	public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
		HeaderIterator headerIterator = response.headerIterator(HTTP.CONN_KEEP_ALIVE);
		HeaderElementIterator iterator = new BasicHeaderElementIterator(headerIterator);
		while (iterator.hasNext()) {
			HeaderElement element = iterator.nextElement();
			String name = element.getName();
			String value = element.getValue();
			if (name != null && name.equalsIgnoreCase("timeout")) {
				return Long.parseLong(value) * 1000;
			}
		}
		// System.out.println("没有保持");
		// 如果没有约定，设置默认时长为60
		return 60 * 10 * 1000;
	}

}
